package com.brucekong.slm.controller.forum;

import com.brucekong.slm.common.PageResult;
import com.brucekong.slm.model.forum.ForumDomain;
import com.brucekong.slm.model.forum.ReplyDomain;
import com.brucekong.slm.service.forum.ForumService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 帖子控制类
 */
@RestController
@RequestMapping("/forum")
@Slf4j
@Api(value = "帖子模块", tags = "帖子模块")
public class ForumController {

    private PageResult pageResult = new PageResult();

    @Autowired
    private ForumService forumService;

    /**
     * 帖子列表
     *
     * @return pageResult 获取帖子列表
     */
    @GetMapping("/list")
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "帖子列表", notes = "获取帖子列表", code = 200, produces = "application/json", responseContainer = "List", response = PageResult.class)
    public PageResult getForumList(@RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                   @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
        log.info("/forum/list");

        PageInfo<ForumDomain> forumServiceForumList = forumService.getForumList(page, limit);

        pageResult.setCode(0);
        pageResult.setMsg(null);
        pageResult.setSuccess(true);
        pageResult.setCount(forumServiceForumList.getTotal());
        pageResult.setData(forumServiceForumList.getList());
        return pageResult;
    }

    /**
     * 回复列表
     *
     * @return pageResult 获取回复列表
     */
    @GetMapping("/replys")
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "回复列表", notes = "获取回复列表", code = 200, produces = "application/json", responseContainer = "List", response = PageResult.class)
    public PageResult getReplyList(@RequestParam(name = "page", required = false, defaultValue = "1") int page,
                                   @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
        log.info("/forum/replys");

        PageInfo<ReplyDomain> replyDomainPageInfo = forumService.getReplyList(page, limit);

        pageResult.setCode(0);
        pageResult.setMsg(null);
        pageResult.setSuccess(true);
        pageResult.setCount(replyDomainPageInfo.getTotal());
        pageResult.setData(replyDomainPageInfo.getList());
        return pageResult;
    }

}